package com.group525.cloudef.controller.admin;

import com.group525.cloudef.common.CommonResult;
import com.group525.cloudef.entity.admin.Article;
import com.group525.cloudef.service.admin.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/article")
@CrossOrigin
@Slf4j
@Api(tags = "文章管理")
//新增、删除、编辑
public class ArticleController {

    @Resource
    ArticleService articleService;

    @PostMapping("AddArticle")
    @ApiOperation(value = "文章新增")
    public CommonResult<?> AddArticle(@RequestBody Article article) {
        return articleService.AddArticle(article);
    }

    @DeleteMapping("deleteArticleById/{id}")
    @ApiOperation(value = "文章删除")
    public CommonResult<?> DeleteArticle(@PathVariable String id) {
        if(articleService.removeById(id)){
            return CommonResult.success();
        }
        return CommonResult.failed();

    }

    @PostMapping("ModifyArticle")
    @ApiOperation(value = "文章修改")
    public CommonResult<?> ModifyArticle(@RequestBody Article article) {
            if (articleService.updateById(article)) {
                return CommonResult.success();
            }
            return CommonResult.failed();


    }

    @GetMapping("getAllArticles")
    @ApiOperation(value = "获取所有文章")
    public CommonResult<?> getAllArticles() {
        if (articleService.list() != null) {
            return CommonResult.success(articleService.list());
        }
        return CommonResult.failed();

    }
}
